﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfApp_MvvmPatten.Base;
using WpfApp_MvvmPatten.Model;

namespace WpfApp_MvvmPatten.ViewModel;

public class CalculateLogic
{
    public CalculateDataModel CalculateDataModel { get; set; }
    public MvvmCommand CalculateCommand { get; set; }
    public MvvmCommand CheckChommand { get; set; }
    public ObservableCollection<CalculateResoutModel> calculateResoutModels { get; set; }

    //删除2，再viewmodel中定义，通过CommandParam传递参数来进行删除集合中的元素
    public MvvmCommand DelCommand { get; set; }

    public CalculateLogic()
    {
        CalculateDataModel = new CalculateDataModel();
        CalculateDataModel.Value1 = 100;
        CalculateDataModel.Value2 = 102;
        CalculateDataModel.PropertyChanged += (sender, e) =>
        {
            if (new string[] { "Value1", "Value2" }.Contains(e.PropertyName))
            {
                CalculateCommand?.ResetCanExecute();
            }
        };
        CalculateCommand = new MvvmCommand(DoCalculate, DoCheck);
        CheckChommand = new MvvmCommand(HandelCheck);

        calculateResoutModels = new ObservableCollection<CalculateResoutModel>();

        DelCommand = new MvvmCommand(param => 
        {
            if (param != null)
            {
                calculateResoutModels.Remove((CalculateResoutModel)param);
            }
        });
    }


    public void DoCalculate(object? param)
    {
        CalculateDataModel.Result = CalculateDataModel.Value1 + CalculateDataModel.Value2;
        calculateResoutModels.Add(new CalculateResoutModel(calculateResoutModels) 
        {
            ProgressStr = $"{CalculateDataModel.Value1}+{CalculateDataModel.Value2} = {CalculateDataModel.Result}",
            State = 0
        });
    }

    public bool DoCheck()
    {
        return CalculateDataModel.Value1 > 0 && CalculateDataModel.Value2 > 0;
    }

    public void HandelCheck(object? param)
    {
        CalculateCommand.ResetCanExecute();
    }
}
